﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using HandlingInput;
using GameCore;

namespace GameObject
{
    public class Map: VisibleGameObject
    {
        int[,] Matrix;
        int cellWidth;
        int CellHeight;
        int nCellWidth;
        int nCellHeight;

        int[,] MatrixD;

        public int[,] XDMatrix
        {
            get { return MatrixD; }
            set { MatrixD = value; }
        }
        
        public void Init(int[,] maTrix,Texture2D[] l_texture, int cellwidth, int cellheight,int ncellWidth,int ncellHeight)
        {
            this.cellWidth = cellwidth;
            this.CellHeight = cellheight;
            this.nCellHeight = ncellHeight;
            this.nCellWidth = ncellWidth;

            this.X = 0;
            this.Y = 0;
            this.Width = cellWidth * nCellWidth - cellWidth/2;
            this.Height = CellHeight * nCellHeight/2 - CellHeight/2;

            Matrix = maTrix;

            _nsprite = ncellHeight * ncellWidth;
            _sprite = new Sprite[_nsprite];
            _lsprite = new List<Vector2>();

            for (int m = 0; m < nCellHeight; m++)
            {
                for (int n = 0; n < nCellWidth; n++)
                {
                    Texture2D[] ls = new Texture2D[1];
                    ls[0] = l_texture[Matrix[m,n]];
                    _sprite[m * nCellWidth + n] = new Sprite(0, 0, 0, 0, ls);
                }
            }

          //  KhoiTaoMatranTrongLuong();
        }
        public override void Update(GameTime gameTime)
        {
            if (_lsprite.Count == 0)
            {
                for (int m = 0; m < nCellHeight; m++)
                {
                    for (int n = 0; n < nCellWidth; n++)
                    {
                        if (m % 2 == 0)
                        {
                            _sprite[m * nCellWidth + n].Update(gameTime,
                                0, n * cellWidth, (m-1) * CellHeight / 2,
                                cellWidth, CellHeight);

                        }
                        else
                        {
                            _sprite[m * nCellWidth + n].Update(gameTime,
                                0, n * cellWidth - cellWidth / 2, (m - 1) * CellHeight / 2,
                                cellWidth, CellHeight);


                        }
                        _lsprite.Add(new Vector2(m * nCellWidth + n, 0));
                    }
                }
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //spriteBatch.Begin();
            //for (int m = 0; m < nCellHeight; m++)
            //{
            //    for (int n = 0; n < nCellWidth; n++)
            //    {
            //        if (m % 2 == 0)
            //        {
            //            spriteBatch.Draw(_arrTexture2D[A[m, n]], new Vector2(n * 96, m * 24 ), new Rectangle(0, 0, _arrTexture2D[A[m, n]].Width, _arrTexture2D[A[m, n]].Height), Color.White);
            //        }
            //        else
            //        {
            //            spriteBatch.Draw(_arrTexture2D[A[m, n]], new Vector2(n * 96 - 48, m * 24), new Rectangle(0, 0, _arrTexture2D[A[m, n]].Width, _arrTexture2D[A[m, n]].Height), Color.White);
            //        }
            //    }
            //}
            //base.Draw(gameTime, spriteBatch);
        }
        public override VisibleGameObject Clone()
        {
            VisibleGameObject newObj = new Map();
            newObj._lsprite = this._lsprite;
            newObj._nsprite = this._nsprite;
            newObj._sprite = new Sprite[_nsprite];
            for (int i = 0; i < _nsprite; i++)
            {
                newObj._sprite[i] = this._sprite[i].Clone();
            }
            newObj.X = this.X;
            newObj.Y = this.Y;
            newObj.Height = this.Height;
            newObj.Width = this.Width;

            ((Map)newObj).XDMatrix = this.XDMatrix;
            ((Map)newObj).Matrix = this.Matrix;
            ((Map)newObj).cellWidth = this.cellWidth;
            ((Map)newObj).CellHeight = this.CellHeight;
            ((Map)newObj).nCellWidth = this.nCellWidth;
            ((Map)newObj).nCellHeight = this.nCellHeight;

            return newObj;
        }

        private void KhoiTaoMatranTrongLuong()
        {
            MatrixD = new int[nCellWidth * nCellWidth, nCellHeight * nCellHeight];

            for (int i = 0; i < nCellHeight * nCellWidth; i++)
            {
                int n = i / nCellWidth;
                int m = i % nCellWidth;

                int oTrenXuongTraiNamDuoi = 0;
                int oTrenXuongTraiNamTren = 0;
                int oTraiXuongPhaiNamTren = 0;
                int oTraiXuongPhaiNamDuoi = 0;


                //ô nghiêng tren xuong sang phai nằm duoi
                if (m + 1 < nCellHeight && n + 1 < nCellWidth && Matrix[m + 1, n + 1] != 0)
                {
                    MatrixD[i, (m + 1) + nCellWidth * (n + 1)] = 100;
                    oTraiXuongPhaiNamDuoi = 1;
                }
                //ô nghiêng tren xuong sang phai nằm tren
                if (m - 1 >= 0 && n - 1 >= 0 && Matrix[m - 1, n - 1] != 0)
                {
                    MatrixD[i, (m - 1) + nCellWidth * (n - 1)] = 100;
                    oTraiXuongPhaiNamTren = 1;
                }

                //duong nghieng tren xuong sang trai nằm tren
                if (m + 1 < nCellHeight && n - 1 >= 0 && Matrix[m + 1, n - 1] != 0)
                {
                    MatrixD[i, (m + 1) + nCellWidth * (n - 1)] = 100;
                    oTrenXuongTraiNamTren = 1;
                }
                //duong nghieng tren xuong sang trai ô nằm duoi
                if (m - 1 >= 0 && n + 1 < nCellWidth && Matrix[m - 1, n + 1] != 0)
                {
                    MatrixD[i, (m - 1) + nCellWidth * (n + 1)] = 100;
                    oTrenXuongTraiNamDuoi = 1;
                }

                //dường ngang ô huong phai
                if (m + 1 < nCellHeight && Matrix[m + 1, n] != 0)
                {
                    //2 o phai khong co vat can
                    if (oTraiXuongPhaiNamDuoi == 1 && oTrenXuongTraiNamTren == 1)
                    {
                        MatrixD[i, (m + 1) + nCellWidth * (n)] = 100;
                    }
                }
                //đường ngang ô huong trai
                if (m - 1 >= 0 && Matrix[m - 1, n] != 0)
                {
                    if (oTraiXuongPhaiNamTren == 1 && oTrenXuongTraiNamDuoi == 1)
                    {
                        MatrixD[i, (m - 1) + nCellWidth * (n)] = 100;
                    }
                }

                //đường doc ô huong len
                if (m - 2 >= 0 && Matrix[m, n - 2] != 0)
                {
                    if (oTraiXuongPhaiNamTren == 1 && oTrenXuongTraiNamTren == 1)
                    {
                        MatrixD[i, (m) + nCellWidth * (n - 2)] = 100;
                    }
                }
                //đường doc ô huong xuong
                if (n + 2 < nCellWidth && Matrix[m, n + 2] != 0)
                {
                    if (oTraiXuongPhaiNamDuoi == 1 && oTrenXuongTraiNamDuoi == 1)
                    {
                        MatrixD[i, (m) + nCellWidth * (n + 2)] = 100;
                    }
                }
            }
        }
     
    }
}
